/**
 * 
 */
package sailing.ui.model;

import java.util.List;

import org.jdesktop.application.SingleFrameApplication;

import sailing.entity.BillsDetail;
import sailing.entity.BillsDetail;
import sailing.managers.BillsDetailManager;
import sailing.utils.Utils;
import cjlite.utils.Lists;

/**
 * @author simon
 * 
 */
public class BillsDetailTableModel extends SailingTableModel {

	public BillsDetailTableModel(SingleFrameApplication application) {
		super(application);
		// TODO Auto-generated constructor stub
	}

	String[] columnName = { "时间", "凭证号", "借或贷", "摘要", "对方科目", "现金支票号码", "转账支票号码", "借方金额","贷方金额", "余额", "编辑操作" };
	private boolean showEditBtn = false;

	private List<BillsDetail> billsDetailAccountList = Lists.newArrayList();

	@Override
	public void fireTableDataChanged() {
		this.updateModel();
		super.fireTableDataChanged();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getRowCount()
	 */
	@Override
	public int getRowCount() {
		return billsDetailAccountList.size();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getColumnCount()
	 */
	@Override
	public int getColumnCount() {
		return showEditBtn? 11:10;
	}

	@Override
	public String getColumnName(int column) {
		return columnName[column];
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getValueAt(int, int)
	 */
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		if (rowIndex >= this.billsDetailAccountList.size()) {
			return "";
		}

		BillsDetail b = this.billsDetailAccountList.get(rowIndex);
		switch (columnIndex) {
		case 0:
			return  Utils.dateFormat(b.getDateTime());
		case 1:
			return b.getCertificateNum();
		case 2:
			return b.getBankDepositsAccountBORL();
		case 3:
			return b.getBankDepositsSummary();
		case 4:
			return b.getOppositeAccount();
		case 5:
			return b.getCashAccountNumber();
		case 6:
			return b.getGirobackNumber();
		case 7:
			return b.getBorrowBalance();
		case 8:
			return b.getLoanAmount();
		case 9:
			return b.getAccountRemainingSum();
		case 10:
			return b;
		}
		return "";
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return showEditBtn && columnIndex == 2;
	}

	@Override
	public void updateModel() {
		BillsDetailManager bm = this.getApplication().getContext().getIoc().getInstance(BillsDetailManager.class);
		billsDetailAccountList.clear();
		billsDetailAccountList.addAll(bm.getAllBillsDetailList());
	}
	

	public void showEditBtn(boolean selected) {
		showEditBtn = selected;
	}
	
	public void updateModel(List<BillsDetail> cvList) {
		billsDetailAccountList.clear();
		billsDetailAccountList.addAll(cvList);
		super.fireTableDataChanged();
	}

	public List<BillsDetail> getDataList() {
		return this.billsDetailAccountList;
	}

}
